package cn.tansanqinger.ThreadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NewSingleThreadExecutorDemo {
    public static void main(String[] args) {
        //单线程的线程池，线程异常结束，会创建一个新的线程，能确保任务按提交顺序执行
        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
        for (int i = 0; i <3; i++) {
            final int index = i;
            singleThreadPool.execute(() -> {
                if (index == 1) {
                    throw new RuntimeException("线程执行出现异常");
                }
                System.out.println(Thread.currentThread().getName());
            });
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {}
        System.out.println("10毫秒后...");
        //关闭线程池
        singleThreadPool.shutdown();
    }

}
